package com.gzwanchuang.lszg.module.account;
/**
 * 文件名 : AllowLogFragment
 * 创建者 : MangoWang
 * 创建日期 : 2019/11/27 13:49
 * 微信 : MangoWaWang
 * 邮箱 : 763482205@qq.com
 * 描述 : TODO
 */

import android.os.Bundle;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.gzwanchuang.lszg.R;
import com.gzwanchuang.lszg.adapter.TiXianProgressAdapter;
import com.gzwanchuang.lszg.base.BaseFragment;
import com.gzwanchuang.lszg.help.UserHelper;
import com.gzwanchuang.lszg.net.Apis;
import com.gzwanchuang.lszg.net.NetworkManager;
import com.gzwanchuang.lszg.net.WebApis;
import com.gzwanchuang.lszg.net.interfaces.OnRequestCallBack;
import com.gzwanchuang.lszg.proto.Withdraw;
import com.gzwanchuang.lszg.web.ui.WebViewActivity;
import com.google.protobuf.InvalidProtocolBufferException;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by AndroidIntexh1 on 2018/6/4.
 */

public class TiXianProgressFragment extends BaseFragment {
    Unbinder unbind;
    @BindView(R.id.fragment_recycleView)
    RecyclerView mRecycleView;
    private TiXianProgressAdapter mAdapter;
    private List<Withdraw.withdraw_detail> datas = new ArrayList<>();
    private int page = 1; // 当前页数
    private int pagesize = 10;


    public static TiXianProgressFragment NewInstance(String instanceType) {
        Bundle bundle = new Bundle();
        bundle.putString("walletType", instanceType);
        TiXianProgressFragment messageTypeFragment = new TiXianProgressFragment();
        messageTypeFragment.setArguments(bundle);
        return messageTypeFragment;
    }


    /**
     * 获取布局文件
     *
     * @return
     */
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_recycleview;
    }


    /**
     * 初始化操作步骤
     */
    @Override
    protected void init() {
        unbind = ButterKnife.bind(this, rootView);
    }

    /**
     * 初始化界面
     */
    @Override
    protected void initView() {
        initRecycle();
        loadData();
    }

    /**
     * 加载数据
     */
    private void loadData() {
        Withdraw.withdraw_list.Builder builder = Withdraw.withdraw_list.newBuilder();
        builder.setMemberId(UserHelper.getUser().getMember_id()).setWalletType(getArguments().getString("walletType"));
        NetworkManager.INSTANCE.post(Apis.withdraw_list, builder.build().toByteArray(), page, new OnRequestCallBack() {
            @Override
            public void onOk(byte[] response) throws InvalidProtocolBufferException {
                List<Withdraw.withdraw_detail> lists =
                        Withdraw.withdraw_list.parseFrom(response).getListList();
                mAdapter.addData(lists);
                int size = lists.size();
                if (size < 10) {
                    mAdapter.getLoadMoreModule().loadMoreEnd(false);
                } else {
                    mAdapter.getLoadMoreModule().loadMoreComplete();
                }

            }

            @Override
            public void onError(int errorCode, String errorMessage) {
                showToast(errorMessage);
            }
        });
    }



    /**
     * 实例化线性底部RecyclerView
     * *
     */
    private void initRecycle() {
        mRecycleView.setLayoutManager(new LinearLayoutManager(mContext));
        mRecycleView.setAdapter(mAdapter = new TiXianProgressAdapter(mContext, datas));
        mAdapter.setEmptyView(R.layout.recycle_empty);
        // 每个item的点击事件
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            WebViewActivity.startActivity(mContext, WebApis.cashWithdrawal_detail+"?pdc_id=" + datas.get(position).getPdcId());

        });
        mAdapter.getLoadMoreModule().setOnLoadMoreListener(() -> {
            page = mAdapter.getData().size() / pagesize + 1;
            loadData();
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unbind.unbind();
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbind = ButterKnife.bind(this, rootView);
        return rootView;
    }


}
